import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import {checktoken} from '@/apis/users'


Vue.use(VueRouter)

const routes = [
  //登录
  {path: '/',name: 'Login',component: Login},

  //首页
  {path: '/home',name: 'Home',component: ()=>import('../views/Home.vue'),

    //二级路由
    children:[
   /* ==========================================后台首页========================================= */
    { path: '/home/main', name: 'Home/main',component: ()=>import('../views/homemodules/HomeMain.vue')},

   /* ==========================================帐号管理========================================= */
    {path: '/home/accountlist', name: 'Home/accountlist', component: ()=>import('../views/homemodules/account/AccountList.vue')}, //帐号列表
    {path: '/home/addaccount',name: 'Home/addaccount', component: ()=>import('../views/homemodules/account/AddAccount.vue')},//添加帐号
    {path: '/home/changepwd', name: 'Home/changepwd', component: ()=>import('../views/homemodules/account/ChangePwd.vue') },//更改密码

   /* ==========================================订单管理========================================= */
    {path: '/home/ordermanager',name: 'Home/ordermanager',component: ()=>import('../views/homemodules/OrderManager.vue')},

   /* ==========================================店铺管理========================================= */
    {path: '/home/shopmanager',name: 'Home/shopmanager', component: ()=>import('../views/homemodules/ShopManager.vue')},

   /* ==========================================商品管理========================================= */
    {path: '/home/goodslist',name: 'Home/goodslist', component: ()=>import('../views/homemodules/goods/GoodsList.vue')},//商品列表
    {path: '/home/goodsadd',name: 'Home/goodsadd',component: ()=>import('../views/homemodules/goods/GoodsAdd.vue')},//商品添加
    {path: '/home/goodsclass',name: 'Home/goodsclass',component: ()=>import('../views/homemodules/goods/GoodsClass.vue')},//商品分类

   /* ==========================================销售统计========================================= */
    {path: '/home/goodcensus',name: 'Home/goodcensus',component: ()=>import('../views/homemodules/census/GoodCensus.vue')},//商品统计
    {path: '/home/ordercensus',name: 'Home/ordercensus',component: ()=>import('../views/homemodules/census/OrderCensus.vue')},//订单统计
   
   /* ==========================================个人中心========================================= */
    {path: '/home/personcenter',name: 'Home/personcenter',component: ()=>import('../views/homemodules/PersonCenter.vue')}
  ]},

   /* ==========================================错误路径========================================= */
  {path:'*',component:()=>import('../views/Error.vue')}
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path!='/'){
    checktoken({token:localStorage.token}).then(res=>{
      // console.log(res);
      if(res.data.code==0)next()
      else{
        next('/')
      }
    })
  }else{
    next()
  }
})

export default router
